﻿Imports System.Reflection

''' <summary>
''' プロパティアーカイバー
''' </summary>
''' <remarks></remarks>
Friend NotInheritable Class PropertyArchiver

    Private Sub New()
    End Sub

    ''' <summary>
    ''' プロパティアーカイブを作成します
    ''' </summary>
    ''' <param name="instance"></param>
    ''' <param name="t"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function Create(instance As Object, t As Type) As IEnumerable(Of PropertyArchive)
        Dim def = t.ToEntityDefinition

        Dim myProps = def.LiteralProperties.Select(Function(c) PropertyArchiver.CreateLiteralArchive(instance, c))
        Dim lowerProps = def.LowerProperties.Select(Function(c) PropertyArchiver.CreateCollectionArchive(instance, c))

        Return myProps.Union(lowerProps)
    End Function

    ''' <summary>
    ''' リテラルプロパティアーカイブを生成します
    ''' </summary>
    ''' <param name="instance"></param>
    ''' <param name="prop"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Shared Function CreateLiteralArchive(instance As Object, prop As LiteralProperty) As PropertyArchive
        Dim arch As New LiteralPropertyArchive

        arch.PropertyInfo = prop
        arch.Regist(instance)

        Return arch
    End Function

    ''' <summary>
    ''' コレクションプロパティアーカイブを作成します
    ''' </summary>
    ''' <param name="instance"></param>
    ''' <param name="prop"></param>
    ''' <returns></returns>
    ''' <remarks>子</remarks>
    Private Shared Function CreateCollectionArchive(instance As Object, prop As PropertyInfo) As PropertyArchive
        Dim arch As New CollectionPropertyArchive

        arch.PropertyInfo = prop
        arch.Regist(instance)

        Return arch
    End Function
End Class
